// Copyright (c) Microsoft Corporation. 
// Licensed under the MIT license.

#include "SimplygonPluginMeshReductionModule.h"
#include "SimplygonReduction.h"
#include "Modules/ModuleManager.h"
#include "TelemetryManager.h"
#include "Features/IModularFeatures.h"

class FSimplygonPluginMeshReductionModule : public ISimplygonPluginMeshReductionModule
{
	public:
	/** IModuleInterface implementation */
	virtual void StartupModule() override;
	virtual void ShutdownModule() override;
	virtual ISimplygonReduction* GetSimplygonReduction() const override;
	virtual ISimplygonReduction* GetDistributedMeshMerging() const override;

	/** IMeshReduction implementation */
	virtual class IMeshReduction* GetStaticMeshReductionInterface() override;
	virtual class IMeshReduction* GetSkeletalMeshReductionInterface() override;
	virtual class IMeshMerging* GetMeshMergingInterface() override;
	virtual class IMeshMerging* GetDistributedMeshMergingInterface() override;
	virtual FString GetName() override;
};

IMPLEMENT_MODULE( FSimplygonPluginMeshReductionModule, SimplygonPluginMeshReduction )

static TUniquePtr<ISimplygonReduction> GSimplygonMeshReduction;
static TUniquePtr<ISimplygonReduction> GSimplygonMeshReductionDistributedMeshMerging;

ISimplygonReduction* FSimplygonPluginMeshReductionModule::GetSimplygonReduction() const
{
	return GSimplygonMeshReduction.Get();
}

ISimplygonReduction* FSimplygonPluginMeshReductionModule::GetDistributedMeshMerging() const
{
	return GSimplygonMeshReductionDistributedMeshMerging.Get();
}

void FSimplygonPluginMeshReductionModule::StartupModule()
{
	GSimplygonMeshReduction.Reset(ISimplygonReduction::Create());
	GSimplygonMeshReductionDistributedMeshMerging.Reset(ISimplygonReduction::Create(true));
	IModularFeatures::Get().RegisterModularFeature( IMeshReductionModule::GetModularFeatureName(), this );
	FTelemetryManager::Initialize();

}

void FSimplygonPluginMeshReductionModule::ShutdownModule()
{
	IModularFeatures::Get().UnregisterModularFeature( IMeshReductionModule::GetModularFeatureName(), this );
	GSimplygonMeshReduction.Reset(nullptr);
	GSimplygonMeshReductionDistributedMeshMerging.Reset(nullptr);
	FTelemetryManager::Get().Shutdown();
}

IMeshReduction* FSimplygonPluginMeshReductionModule::GetStaticMeshReductionInterface()
{
	return GSimplygonMeshReduction.Get();
}

IMeshReduction* FSimplygonPluginMeshReductionModule::GetSkeletalMeshReductionInterface()
{
	return GSimplygonMeshReduction.Get();
}

IMeshMerging* FSimplygonPluginMeshReductionModule::GetMeshMergingInterface()
{
	return GSimplygonMeshReduction.Get();
}

IMeshMerging* FSimplygonPluginMeshReductionModule::GetDistributedMeshMergingInterface()
{
	return GSimplygonMeshReductionDistributedMeshMerging.Get();
}

FString FSimplygonPluginMeshReductionModule::GetName()
{
	return FString( "SimplygonPluginMeshReduction" );
}
